#include "GY_LineStyleComboBox.h"

GY_LineStyleComboBox::GY_LineStyleComboBox(QWidget* parent)
    : QComboBox(parent)
{

}

/**
 * @brief addLineItem 添加线条Item
 * @param lineName 线条样式名称
 * @param style 样式数值（16进制数）
 * @param with 线条宽度
 */
void GY_LineStyleComboBox::addLineDataItem(const QString &lineName, const QString &style, int &width)
{
    if(lineName.isNull() || style.isNull())
    {
        return;
    }
    if(width==0)
    {
        width = 1;
    }
    bool ok;
    int num = style.toInt(&ok,16);
    QString strStyle = QString::number(num,2); //将样式数值转换为二进制

    QWidget* pWidget = new QWidget(); //创建新页面放置线条
    pWidget->setStyleSheet("background-color:transparent;"); //设置透明背景
    pWidget->setFixedSize(35,20);

    QHBoxLayout* pLayout = new QHBoxLayout(); //布局垂直位置
    int lineWidth = pWidget->height()/2-width;
    pLayout->setContentsMargins(0,lineWidth,0,lineWidth); //设置边距
    pLayout->setSpacing(0); //设置间距
    for(int i=0;i<strStyle.size();i++)
    {
        QLabel* pLabel = new QLabel(pWidget);
        if(strStyle[i]=="1")
        {
            pLabel->setStyleSheet("QLabel{background-color:black;}");
        }
        else {
            pLabel->setStyleSheet("QLabel{background-color:transparent;}");
        }
        pLayout->addWidget(pLabel);
    }
    pWidget->setLayout(pLayout);

    QPixmap pixmap = pWidget->grab(pWidget->rect()); //获取线条截图
    this->addItem(QIcon(pixmap),lineName);
    this->setIconSize(pWidget->size());
    this->setSizeAdjustPolicy(QComboBox::AdjustToContents);
}
